var MGTOOL = {
	getEvent : function(e){
		return e || window.event || top.window.event;
	},
	getTarget : function(e){
		e = this.getEvent(e);
		return e.target || e.srcElement;
	},	
	stopPropagation : function(e){
		e = this.getEvent(e);
		if(e.stopPropagation){
			e.stopPropagation();
		}else{
			e.cancelBubble = true;
		}
	},
	preventDefault : function(e){
		e = this.getEvent(e);
		if(e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		}
	},
	stopEvent : function(e){
		e = this.getEvent(e);
		this.stopPropagation(e);
		this.preventDefault(e);
	},
	getkeycode : function(e){
		e = this.getEvent(e);
		var code = e.keyCode ? e.keyCode : e.witch ? e.witch : null;
		return code;
	},
	boxModel : (function(){
		var div = document.createElement("div");
		div.style.width = div.style.paddingLeft = "1px";
		document.body.appendChild(div);
		var boxModel = div.offsetWidth === 2;
		document.body.removeChild(div).style.display = "none";
		div = null;
		return boxModel;
    })(),
	getoffset : function(el){
		if(!el || !el.ownerDocument){return null;}
		var pos = {
			left : 0,
			top : 0
		};
		if("getBoundingClientRect" in document.documentElement){
		var box = el.getBoundingClientRect(),
			root = el.ownerDocument,
			body = root.body,
			docelem = root.documentElement,
			clTop = docelem.clientTop || body.clientTop || 0,
			clLeft = docelem.clientLeft || body.clientLeft || 0;
			pos.left = box.left +(self.pageXOffset || this.boxModel && docelem.scrollLeft || body.scrollLeft) - clLeft;
			pos.top = box.top + (self.pageYOffset || this.boxModel && docelem.scrollTop || body.scrollTop) -clTop;
			return pos;
		}else{
			var x = 0, y = 0;
			do{x += el.offsetLeft;y += el.offsetTop;}while((el=el.offsetParent));
			pos.left = x;
			pos.top = y;
			return pos;
		}
		
    },
	addEvent : function(obj, type, fn){
		if(obj.addEventListener){
			obj.addEventListener(type, fn, false);
		}else if(obj.attachEvent){
			obj.attachEvent("on" + type, fn);
		}else{
			obj["on" + type] = fn;
		}
	},
	delEvent : function(obj, type, fn){
		if(obj.removeEventListener){
			obj.removeEventListener(type, fn, false);
		}else if(obj.detachEvent){
			obj.detachEvent("on" + type, fn);
		}else{
			obj["on" + type] = null;
		}
	},	
	$id : function(id){
	    var doc = window.document;
		return doc.getElementById(id);
	},
	getbyClass : function(parent, tag, name){
		parent = parent || document;
		tag = tag || "*";
		var arr = [];
		var reg = new RegExp("(^|\\s)" +name+ "(\\s|$)");
		var ts = parent.getElementsByTagName(tag);
		for(var i=0, j=ts.length; i<j; i++){
			 if(reg.test(ts[i].className)){
				arr.push(ts[i]);
			 }
		}
		return arr;

	},
	hasClass : function(obj, sClass){
		var classname = obj.className,
			re = new RegExp("(?:^|\\s)" + sClass + "(?:\\s|$)");
		return obj && sClass && classname && re.test(classname);
	},
	removeClass : function(obj, sClass){
		var reg = new RegExp("(^|\\s)" +sClass+ "(\\s|$)");
		if(obj.className){
		    obj.className = (obj.className.split(' ').length<=2)? obj.className.replace(reg, '') : obj.className.replace(reg, ' ');
		}else{
		    obj.className = obj.className.replace(reg, '');
		}
		
	},
	addClass: function(obj, sClass){
	    if(this.hasClass(obj, sClass)){ return;}
	    if(obj.className.length){
			var cln = obj.className.split(" ");
			cln.push(sClass);
			obj.className = cln.join(" ");
		}else{
		    obj.className = sClass;
		}
	},
	prev : function(elem){
		do{
		  elem = elem.previousSibling
		}while(elem && elem.nodeType !=1)
		return elem;
	},
	next : function(elem){
		do{
		  elem = elem.nextSibling
		}while(elem && elem.nodeType !=1)
		return elem;
	},
	clone : function (obj){
		function F(){}
		F.prototype = obj;
		return new F();
    },
	bind : function(fn,context){
	    var slice = Array.prototype.slice;
	    var args = slice.call(arguments,2);
		return function(){
		    var secargs = slice.call(arguments,0);
			var finalargs = args.concat(secargs);
			return fn.apply(context || window, finalargs)
		};
	},
	extend : function(subClass, superClass){
	    var F = function(){};
		F.prototype = superClass.prototype;
		subClass.prototype = new F();
		subClass.prototype.constructor = subClass;
		subClass.superclass = superClass.prototype;
		if(superClass.prototype.constructor == Object.prototype.constructor){
		    superClass.prototype.constructor = superClass;
		}
	},
	contains : function(a, b){
		if(!a) return false;
		var iscontain = false;
		if(a.contains){
			iscontain = a.contains(b);
		}else{
			iscontain = a.compareDocumentPosition(b) == 20 ? true : false;
		}
		return iscontain;
    },
	isJson : function(obj){
	    return typeof(obj)==='object' && Object.prototype.toString.call(obj)==='[object Object]' && !obj.length;
	},
	isArray : function(obj){
	    return Object.prototype.toString.call(obj)==='[object Array]';
	},
	ajax : (function(){
		//do handel
	    function handle(o, callback){
		    var poll = setInterval(function(){
			    if(o&&o.readyState == 4){
				    window.clearInterval(poll);
					if(callback){
					    callback(o);
					}
				}								
			}, 50);
		}
		
		// get xhr
		var getxhr = function(){
		    if(typeof XMLHttpRequest != 'undefined'){
			    getxhr = function(){
				    return new XMLHttpRequest();
				};
			}else if(typeof ActiveXObject !='undefined'){
			    getxhr = function(){
				    return new ActiveXObject(
					    navigator.userAgent.indexOf('MSIE 5') >=0 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'
					);
				};
			}else{
			    getxhr = function(){
				    throw new Error('can not create xhr!');
				};
			}
			return getxhr();
		};
		
		// send msg
		return function(method, uri, callback, postData){
			var http = getxhr();
			http.open(method, uri, true);
			handle(http, callback);
			http.send(postData || null);
			return http;
		};
	})()
	
	
};